package com.sanyang.lawyer.controller.audit;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sanyang.lawyer.ResultApi.CodeMsg;
import com.sanyang.lawyer.ResultApi.ResponseResult;
import com.sanyang.lawyer.entity.Audit;
import com.sanyang.lawyer.entity.Case_audit;
import com.sanyang.lawyer.service.Audit.Audit_closeLawService;
import com.sanyang.lawyer.service.Case.Case_lawService;
import com.sanyang.lawyer.utils.ActivitiUtils;
import org.activiti.spring.integration.Activiti;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("Audit_closeLaw")
public class Audit_closeLaw {

    @Autowired
    private ObjectMapper objectMapper = new ObjectMapper();
    
    @Autowired
    private ActivitiUtils activitiUtils;
    
    @Autowired
    private Audit_closeLawService closeLawService;
    
    @Autowired
    private Case_lawService lawService;
    

    //结案审核
    @RequestMapping("closeLawStart")
    public ResponseResult<String> closeLawStart(String data){
        Case_audit case_audit = null;
        try {
            case_audit = objectMapper.readValue(data, Case_audit.class);
        } catch (IOException e) {
            //错误返回服务器正忙错误
            ResponseResult.error(CodeMsg.SERVER_BUSY);
        }
        //1:开始流程
        String processid = activitiUtils.start("委托代理人", "my05");
        //2:审核表中添加数据   流程id，提交人，提交备注，    类型id，提交时间。审核状态。
        case_audit.getAuditid().setAuditid(processid);
        closeLawService.insertCloseOne(case_audit);
        //3:案件和审核表中间表添加数据 案件id，审核id。
        closeLawService.insertCase_auditOne(case_audit);
        //4:推行到下一步经理审核
        activitiUtils.Mycomplete(processid);
        return ResponseResult.success("成功添加审核！~");
    }
    
    
    //完成结案审核
    @RequestMapping("completeProcess")
    public ResponseResult<String> completeProcess(String data){
        Audit audit = null;
        try {
            audit = objectMapper.readValue(data, Audit.class);
        } catch (IOException e) {
            //错误返回服务器正忙错误
            ResponseResult.error(CodeMsg.SERVER_BUSY);
        }
        //1：根据审核id完成审核
        activitiUtils.Mycomplete(audit.getAuditid());
        //2：审核表数据修改
        closeLawService.completeProcess(audit);
        //3：根据审核id关联案件，将案件表数据修改 将案件的状态修改为已结案
        lawService.updateCase_status(audit.getAuditid());
        return ResponseResult.success("完成审核！~");
    }
    
}
